import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) 
{
    return originalPush.call(this, location).catch(err => err)
}

export const constantRoutes = [
    {
        path: '/',
        redirect: '/login',
        meta: {
          title: '登录',
        },
    },
    {
        path: '/login',
        component: () => import('@/views/login/index'),
        hidden: true,
        meta: {
          title: '登录',
        },
    },
    {
        path: '/home',
        component: () => import('@/views/home/index'),
        hidden: true,
        redirect: '/welcome',
        children: [
            {
                path: '/welcome',
                component: () => import('@/views/home/welcome'),
                meta: {
                  title: '欢迎页',
                },
            },
            {
                path: '/users',
                component: () => import('@/views/users/index'),
                meta: {
                  title: '用户',
                },
            },
            {
                path: '/rules',
                component: () => import('@/views/rules/index'),
                meta: {
                  title: '权限',
                },
            },
            {
                path: '/roles',
                component: () => import('@/views/roles/index'),
                meta: {
                  title: '角色',
                },
            },
            {
                path: '/productsTypes',
                component: () => import('@/views/products/productsType'),
                meta: {
                  title: '产品类型',
                },
            },
            {
                path: '/productsParams',
                component: () => import('@/views/products/productsParams'),
                meta: {
                  title: '产品参数',
                },
            },
            {
                path: '/products',
                component: () => import('@/views/products/products'),
                meta: {
                  title: '产品',
                },
            },
            {
                path: '/products/add',
                component: () => import('@/views/products/productsAdd'),
                meta: {
                  title: '产品新增',
                },
            },
            {
                path: '/orders',
                component: () => import('@/views/order/orders'),
                meta: {
                  title: '订单',
                },
            },
            {
                path: '/counts',
                component: () => import('@/views/counts/counts'),
                meta: {
                  title: '统计',
                },
            },
            {
                path: '/default',
                component: () => import('@/views/default'),
                meta: {
                  title: '示例',
                },
            },
        ]
    }

]

const createRouter = () => new Router({
    routes: constantRoutes
})

const router = createRouter()

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
    // to 将要访问的路径
    // from 代表从哪个路径来
    // next 是一个函数 next() ：直接放行；  next("/login") ： 跳转到登录页面；
    // console.log(to, from, next)
    // 获取 token 
    const tokenStr = window.sessionStorage.getItem("token");
    document.title = to.meta.title
    if (to.path === '/login' && !tokenStr) return next();

    // 如果没有 token 去登录
    if (!tokenStr) {
        return next("/login");
    }
    // 有了 token 还去 login 页面直接跳转到 home
    if (to.path === '/login') {
        return next(window.sessionStorage.getItem("activePath") || '/home');
    }
    next();
    
})

export default router